<template>
  <div class="sub-nav-wrap" :class="{'fixed': fixed == 1, 'fbottom': fixed == 2}" ref="wrapRef">
    <div v-if="title" class="nav-title">
      <div class="__title">产品分类</div>
      <div class="__anv">12，3，4，5</div>
    </div>
    <ul ref="ulRef" v-if="isPC" :class="{'hasTitle': title !== ''}">
      <li v-for="it in list"  :key="it.index" :class="{'active': it.index == index}" @click="handleChange(it)">{{it.name}}</li>
    </ul>
    <div class="nav-content-wrap" ref="contentRef">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  data () {
    return {
      fixed: 0
    };
  },

  components: {},
  props: {
    list:{
        type: Array,
        require: true,
    },
    index:{
      type: Number,
      default: 1,
    },
    title: {
      type: String,
      default: ''
    }
  },
  mounted() {
    window.addEventListener('scroll', this.getScroll);
  },
  computed: {
    ...mapGetters({
      isPC:'isPC'
    })
  },
  watch: {},
  methods: {
    handleChange(a_item){
      console.log(a_item.index)
      if(a_item.index == this.index) return 
      let content = this.$refs.contentRef
      let childNodes = content.childNodes[0].childNodes
      let nodeList = []
      childNodes.forEach(v=>{
        if(v.nodeName != '#text')nodeList.push(v)
      })
      let _dom = nodeList[a_item.index - 1]
      
      const _this = this
      const windowH = window.innerHeight // 浏览器窗口高度
      const h = _dom.offsetHeight // 模块内容高度
      const t = _dom.offsetTop // 模块相对于内容顶部的距离
      
      const top = t
      const scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop // 滚动条距离顶部高度
      
      window.scrollTo(0, top + 550)


      // window.s





      // console.log(_dom.getBoundingClientRect());
      // let top = _dom.getBoundingClientRect().top
      // // nodeList[a_item.index - 1].scrollTop = '0px'
      // console.log(top)
      // kljljlsjiellsieljof
      // window.scrollTo(0, top)
      
    },
    getScroll(e){
      if(!this.isPC) return
      
      let HEIGHT = 74
      let wrap = this.$refs.wrapRef
      let content = this.$refs.contentRef
      let ul = this.$refs.ulRef
      let { top, bottom, height } = wrap.getBoundingClientRect();
      let ulRect = ul.getBoundingClientRect()
      let ulHeight = ulRect.height
      if(top < HEIGHT ){
          if(top + height < ulHeight + HEIGHT){
            this.fixed = 2
          }else{
            this.fixed = 1
          }
      }else {
        this.fixed = 0
      }
      if(content.childNodes[0]){
        let childNodes = content.childNodes[0].childNodes
        let nodeList = []
        childNodes.forEach(v=>{
          if(v.nodeName != '#text')nodeList.push(v)
        })
        let i = 0
        nodeList.forEach(v => {
          let { top } = v.getBoundingClientRect()
          if(top < HEIGHT + 100){
            i++
          }
        });
        if(i == 0) i = 1
        this.$emit('update:index', i)
      }
      
      // console.log(content.childNodes[0].childNodes)
      // this.list.forEach( v => {
        
      // });
    }
    
  },
  destroyed(){
    window.removeEventListener('scroll', this.getScroll)
    this.fixed = 0
  },
}

</script>
<style lang='scss' scoped>
@import "@/style/variables.scss";

.sub-nav-wrap{
  position: relative;
  --width: 260px;
  --height: 40px;
  .nav-title{
    display: flex;
    line-height: var(--height);
    .__title{
      width: var(--width);
      height: var(--height);
      background-color: #2A82E4;
      color: #fff;
      
      text-align: center;
    }
    .__anv{
      padding-left: 3px;
      height: var(--height)
    }
  }

  &>ul{
    position: absolute;
    top: 0;
    left: 0;
    &.hasTitle{
      top: var(--height);
    }
    &>li{
      position: relative;
      width: var(--width);
      height: 81px;
      font-size: 16px;
      line-height: 81px;
      text-align: center;
      border: 1px solid #ccc;
      border-left: none;
      cursor: pointer;
      font-weight: 700;
      &::after{
        content:"";
        position: absolute;
        left: 35px;
        top:38px;
        width: 5px;
        height: 5px;
        background-color: #000;
        border-radius: 10px;
      }
      &.active{
        font-weight: bold;
        color:$primary;
        &:before{
          content:"";
          position: absolute;
          right: -1px;
          top: 0;
          bottom: 0;
          width: 3px;
          background-color: $primary;
        }
      }
      &:nth-child(n+1){
        margin-top: -1px;
      }
    }
    
  }
  .nav-content-wrap{
    flex: 1;
    padding: 50px 0;
    margin-left: calc(var(--width) + 3px);
    // min-height:200px;
    // max-height: 500px;
    // overflow-y: scroll;
  }

  &.fixed{
    &>ul{
      position: fixed;
      top: 74px;
      left:auto;
      background-color: #fff;
    }
    .nav-content-wrap{
      margin-left: 263px;
    }
  }

  &.fbottom{
    &>ul{
      position: absolute;
      bottom: 0;
      top: auto;
    }
    .nav-content-wrap{
      margin-left: 263px;
    }
  }
}
@media screen and (max-width:768px) {
  .sub-nav-wrap{
    // padding: 0 .2rem;
    .nav-content-wrap{
      margin-left: 0;
      padding: .2rem 0;
      .sub-nav-content{
        padding: .2rem 0;
      }
    }
  }
  
}
</style>